﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Evolutility;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;

public partial class MRP_OrderLock : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void UIServer1_DBBeforeUpdate(object sender, UIServer.MRPDataUpdateEventArgs e)
    {
        //string connectstring;
        string setString = e.SetString;
        int id = e.ID;
        string sSQL = "";
        List<OrderMaterial> MaterialList = new List<OrderMaterial>();

        //如果是确认状态。
        if (setString.ToLower().Contains("[status]=2"))
        {
            Database db = DatabaseFactory.CreateDatabase("MRPSqlServer");

            sSQL = string.Format("SELECT MRP_OrderDetail.ProductID,MRP_OrderDetail.Quantity FROM MRP_Order INNER JOIN MRP_OrderDetail ON MRP_Order.ID = MRP_OrderDetail.OrderID where MRP_Order.ID={0}", id);
            //取得MaterialID
            using (IDataReader reader = db.ExecuteReader(CommandType.Text, sSQL))
            {
                while (reader.Read())
                {
                    MaterialList.Add(new OrderMaterial(int.Parse(reader[0].ToString()), int.Parse(reader[1].ToString())));
                }

            }

            foreach (OrderMaterial Material in MaterialList)
            {
                sSQL = string.Format("Exec MRP_OrderConfirmProduct {0},{1}", Material.MaterialID, Material.Count);

                db.ExecuteNonQuery(CommandType.Text, sSQL);

            }

        }

    }
}
